import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { DeptService } from './dept.service';
import { CreateDeptDto } from './dto/create-dept.dto';
import { FindAllDeptDto } from './dto/find-all-dept.dto';
import { UpdateDeptDto } from './dto/update-dept.dto';

@Controller('dept')
export class DeptController {
  constructor(private readonly deptService: DeptService) {}

  @Get()
  findAll(@Query() query: FindAllDeptDto) {
    return this.deptService.findAll(query);
  }

  // 删除单个
  // @Delete(':id')
  // async remove(@Param('id') id: number) {
  //   console.log('remove');

  //   await this.deptService.remove(id);
  //   return '删除成功';
  // }

  @Delete(':ids')
  async removeMany(@Param('ids') ids: string) {
    // 将传入的字符串ids（比如"2,3"）转换为数字数组
    const idArray = ids.split(',').map(Number);
    await this.deptService.removeMany(idArray);
    return '删除成功';
  }

  @Get('options')
  findOptions() {
    return this.deptService.findOptions();
  }

  @Get(':id/form')
  findForm(@Param('id') id: number) {
    return this.deptService.findForm(id);
  }

  @Put(':id')
  async update(@Param('id') id: number, @Body() body: UpdateDeptDto) {
    await this.deptService.update(id, body);
    return '编辑成功';
  }

  @Post()
  async create(@Body() body: CreateDeptDto) {
    await this.deptService.create(body);
    return '创建成功';
  }
}
